<?php

require_once  __DIR__ . '/includes/authenticate.php';
include_once './db_bb_connect.php';

?>

<div id="viewtraininggeninfocontent">
<h4>View General Training Info</h4>


<form>
	
<select class="form-control"  name="season" id="select4">
		
<?php

if (isset($_POST['season'])) {

	$setseason = $_POST['season'];

echo  "<option selected=\"selected\" value=\"$setseason\"> $setseason is selected</option>";
	
} else {
	echo  "<option selected=\"selected\" disabled=\"disabled\"> Select season</option>";
}


$sql = "SELECT DISTINCT season 
FROM traininginfo";

$result = $db->query($sql);

while ($row = $result->fetch_assoc()) {
	$season = $row['season'];

	echo "<option value=\"$season\"> $season </option>";
}



?>
</select>

<br>
		
<?php

if (isset($_POST['season'])) {

	?>

<select class="form-control"  name="date" id="select5">

<?php 

if (isset($_POST['date'])) { 

	$setdate = $_POST['date'];

	echo "<option selected=\"selected\" value=\"$setdate\"> $setdate is selected</option>";

} else {

	echo "<option selected=\"selected\" disabled=\"disabled\"> Select Date of Training you want to show</option>";

	}



$sql2 = "SELECT DISTINCT date 
FROM traininginfo
WHERE season = '$setseason'
ORDER BY date DESC";

$result2 = $db->query($sql2);

while ($row2 = $result2->fetch_assoc()) {
	$date = $row2['date'];

	echo "<option value=\"$date\"> $date </option>";
}

}

?>
</select>

</form>

<br><br>

<?php

if (isset($_POST['date']) && isset($_POST['season'])) {

	$setdate = $_POST['date'];
	$setseason = $_POST['season'];

?>

 <div class="table-responsive">  
  <h4>Training Info of <?= $setdate; ?></h4>             
  <table class="table table-hover">
    <thead>
      <tr>
        <th>Topics discussed</th>
        <th>Comment</th>
      </tr>
    </thead>
    <tbody>
      




<?php
	
	$sql3 = "SELECT subjects, comment
	FROM traininginfo 
	WHERE date = '$setdate'
	AND season = '$setseason'";

	$result3 = $db->query($sql3);

	while ($row3 = $result3->fetch_assoc()) {
		$subjects = $row3['subjects'];
		$comment = $row3['comment'];

	 echo "<tr>
        <td>$subjects</td>
        <td>$comment</td>
      </tr>
      ";
}

?>


    </tbody>
  </table>
  </div>

<?php
}
?>

</div>


<script type="text/javascript">
	
$("#select4").on("change", function(e) {
    postData = $(this.form).serialize();
    $.ajax({
        type: "POST",
        url: "./training_viewgeninfo.php",
        data: postData,
        success: function(data) { 
        	//alert(postData);
            $("#viewtraininggeninfocontent").html(data); 
        },
        error: function (xhr, status, error) {
        	$("#viewtraininggeninfocontent").html(xhr.responseText); 
        }
      }); 
    e.preventDefault();
});  

$("#select5").on("change", function(e) {
    postData = $(this.form).serialize();
    $.ajax({
        type: "POST",
        url: "./training_viewgeninfo.php",
        data: postData,
        success: function(data) { 
        	//alert(postData);
            $("#viewtraininggeninfocontent").html(data); 
        },
        error: function (xhr, status, error) {
        	$("#viewtraininggeninfocontent").html(xhr.responseText); 
        }
      }); 
    e.preventDefault();
});  

</script>